package com.leyou.user.service;

import com.leyou.user.mapper.UserMapper;
import com.leyou.user.pojo.User;
import com.leyou.user.utils.CodecUtils;
import com.leyou.utils.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @author: Mr_Sun
 * @Date: 2018/9/21
 * @Time: 17:32
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private AmqpTemplate amqpTemplate;
    private static final String leyou_user_prefix = "user:verify";

    public Boolean checkUser(String data, Integer type) {
        User record = new User();
        switch (type) {
            case 1:
                record.setUsername(data);
                break;
            case 2:
                record.setPhone(data);
                break;
            default:
                return null;
        }
        return this.userMapper.selectCount(record) == 0;
    }

    public Boolean sendVerifyCode(String phone) {
        try {
            //生成验证码
            String code = NumberUtils.generateCode(6);
            //保存到本地redis
            this.stringRedisTemplate.opsForValue().set(leyou_user_prefix+phone,code,5,TimeUnit.MINUTES);
            //发送消息
            Map<String, String> msg = new HashMap<>();
            msg.put("phone", phone);
            msg.put("code", code);
            this.amqpTemplate.convertAndSend("LEYOU.SMS.EXCHANGE", "sms.verify", msg);
            return true;
        } catch (AmqpException e) {
            e.printStackTrace();
        }
        return false;
    }

    public Boolean register(User user, String code) {
        //查询redis中的验证码
        String cacheCode = this.stringRedisTemplate.opsForValue().get(leyou_user_prefix + user.getPhone());
        //校验验证码
        if (!StringUtils.equals(cacheCode, code)) {
            return false;
        }
        //生成盐
        String salt = CodecUtils.generateSalt();
        user.setSalt(salt);
        // 对密码加密
        user.setPassword(CodecUtils.md5Hex(user.getPassword(), salt));
        //新增用户
        user.setId(null);
        user.setCreated(new Date());
       Boolean b= this.userMapper.insertSelective(user)==1;
        if (b) {
            //删除redis中得验证码
            this.stringRedisTemplate.delete(leyou_user_prefix + user.getPhone());
        }
        return true;
    }

    public User queryUser(String username, String password) {
        // 查询
        User record = new User();
        record.setUsername(username);
        User user = this.userMapper.selectOne(record);
        // 校验用户名
        if (user == null) {
            return null;
        }
        // 校验密码
        if (!user.getPassword().equals(CodecUtils.md5Hex(password, user.getSalt()))) {
            return null;
        }
        // 用户名密码都正确
        return user;
    }
}
